package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsPictureInfo;
import com.ruoyi.system.domain.news.dto.NewsPictureInfoDto;
import com.ruoyi.system.domain.news.query.NewsPictureInfoQuery;
import com.ruoyi.system.domain.news.vo.NewsPictureInfoVo;
import com.ruoyi.system.service.news.NewsPictureInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsPictureInfo")
@Api(value = "newsPictureInfoController", tags  = "图片新闻")
public class NewsPictureInfoController extends BaseController {

	@Resource
	private NewsPictureInfoService newsPictureInfoService;

    @ApiOperation(value = "新增/修改图片新闻信息", notes = "新增/修改图片新闻信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsPictureInfoDto newsPictureInfoDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsPictureInfo newsPictureInfo = new NewsPictureInfo();
		BeanUtils.copyProperties(newsPictureInfoDto,newsPictureInfo);
		if (newsPictureInfo.getId()==null) {
			newsPictureInfo.setCreateBy(loginUser.getUsername());
			newsPictureInfo.setDeptId(loginUser.getDeptId());
			newsPictureInfo.setCreateTime(new Date());
			newsPictureInfo.setUpdateBy(loginUser.getUsername());
			newsPictureInfo.setUpdateTime(new Date());
		} else {
			newsPictureInfo.setUpdateBy(loginUser.getUsername());
			newsPictureInfo.setUpdateTime(new Date());
		}
		boolean result = newsPictureInfoService.saveOrUpdate(newsPictureInfo);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除图片新闻信息", notes = "删除图片新闻信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsPictureInfoService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个图片新闻信息", notes = "获取单个图片新闻信息")
	@GetMapping("/getById")
	public R<NewsPictureInfoVo> getById(@RequestParam(value = "id") Long id) {
		NewsPictureInfoQuery newsPictureInfoQuery = new NewsPictureInfoQuery();
		newsPictureInfoQuery.setId(id);
		List<NewsPictureInfoVo> newsPictureInfoList = newsPictureInfoService.getNewsPictureInfoList(newsPictureInfoQuery);
		if (newsPictureInfoList!=null&&newsPictureInfoList.size()>0) {
			return R.ok(newsPictureInfoList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取图片新闻信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsPictureInfoVo>> list(@RequestBody NewsPictureInfoQuery newsPictureInfoQuery) {
		List<NewsPictureInfoVo> newsPictureInfoList = newsPictureInfoService.getNewsPictureInfoList(newsPictureInfoQuery);
		return R.ok(new PageInfo<>(newsPictureInfoList));
	}
}
